/*
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package de.teamskill.pux.library.image;

import java.awt.image.BufferedImage;

/**
 * InvertImageNode
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class InvertImageNode extends AbstractImageFilter {

	/**
	 * InvertImageNode Constructor
	 * 
	 */
	public InvertImageNode() {
		this("INVERT" + nr++, false);
	}

	/**
	 * InvertImageNode Constructor
	 * 
	 * @param nodeName
	 * @param continuous
	 */
	public InvertImageNode(String nodeName, boolean continuous) {
		super(nodeName, continuous);
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#compute()
	 */
	public void compute() {
		BufferedImage bi = (BufferedImage) in.fetchData();

		BufferedImage oi = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
		int[] samples = new int[bi.getData().getNumBands()];
		LOG_NODE.finer("Found " + samples.length + " bands in image.");
		for (int x = 0; x < bi.getWidth(); x++) {
			for (int y = 0; y < bi.getHeight(); y++) {
				int value = bi.getRGB(x, y);
				int red = 0xff0000 - (0xff0000 & value);
				int green = 0x00ff00 - (0x00ff00 & value);
				int blue = 0x0000ff - (0x0000ff & value);
				int pixel = red | green | blue;
				oi.setRGB(x, y, pixel);
			}
		}
		out.pushData(oi);
	}
}
